package com.hntest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hntest.entity.LoginInfo;
import com.hntest.entity.RealAuth;
import com.hntest.entity.UserBankInfo;
import com.hntest.service.UserBankInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller("userBankInfoController")
public class UserBankInfoController {

    @Autowired
    private UserBankInfoService userBankInfoService;

    @RequestMapping(value = "/toInsertUserBankInfo",method = {RequestMethod.POST,RequestMethod.GET})
    public String toInsertUserBankInfo(UserBankInfo userBankInfo,ModelMap modelMap,HttpServletRequest request){
        HttpSession session = request.getSession(true);
        LoginInfo currUser = (LoginInfo) session.getAttribute("currUser");
        userBankInfo.setLoginInfo(currUser);

        UserBankInfo userBankInfo2 = userBankInfoService.selectUserBankInfoByLoginId(currUser.getLid());
        if (userBankInfo2!=null){
            modelMap.put("userBankInfo",userBankInfo2);
            modelMap.put("ok",false);
        }else {
            //int n = userBankInfoService.insertUserBankInfo(userBankInfo);
             modelMap.put("ok",true);
        }
        return "insertUserBank";
    }

    @RequestMapping(value = "/doInsertUserBankInfo",method = {RequestMethod.POST,RequestMethod.GET})
    public String doInsertUserBankInfo(UserBankInfo userBankInfo,ModelMap modelMap,HttpServletRequest request){
        HttpSession session = request.getSession(true);
        LoginInfo currUser = (LoginInfo) session.getAttribute("currUser");
        userBankInfo.setLoginInfo(currUser);

        System.out.println("-------------------------------->>>>"+userBankInfo.getBankforkname());
        System.out.println(userBankInfo);
        int n = userBankInfoService.insertUserBankInfo(userBankInfo);
        if(n>0){
            UserBankInfo userBankInfo2 = userBankInfoService.selectUserBankInfoByLoginId(currUser.getLid());
            modelMap.put("userBankInfo",userBankInfo2);
            modelMap.put("ok",false);
        }else {
            modelMap.put("ok",true);
        }

        return "insertUserBank";
    }

}
